package models

import (
	"time"
	"github.com/astaxie/beego/orm"
	"strconv"
	"read.com/admin/libs"
)

type ReportDailyStore struct {
	Id            int       `orm:"column(id);auto"`
	ReportDate    time.Time `orm:"column(report_date);type(date)" description:"日报时间"`
	AppId         int       `orm:"column(app_id)" description:"应用ID"`
	UserNew       int       `orm:"column(user_new)" description:"新增用户数"`
	UserLogin     int       `orm:"column(user_login)" description:"登录用户数(活跃用户数)"`
	OrderUser     int       `orm:"column(order_user)" description:"充值用户数"`
	OrderTotal    int       `orm:"column(order_total)" description:"充值总额"`
	OrderCount    int       `orm:"column(order_count)" description:"充值订单数"`
	OrderUserNew  int       `orm:"column(order_user_new)" description:"[新增用户]充值用户数"`
	OrderTotalNew int       `orm:"column(order_total_new)" description:"[新增用户]充值总额"`
	OrderCountNew int       `orm:"column(order_count_new)" description:"[新增用户]充值订单数"`
	D1Retention   int       `orm:"column(d1_retention)" description:"次日留存(新增用户)"`
	D7Retention   int       `orm:"column(d7_retention)" description:"7日留存(新增用户)"`
	D7UserNew     int       `orm:"column(d7_user_new)" description:"7日新增用户数"`
}

func (t *ReportDailyStore) TableName() string {
	return "report_daily_store"
}

func init() {
	orm.RegisterModel(new(ReportDailyStore))
}

func GetDailyReportList(page int,pageSize int,start string,end string)([]map[string]string,int64,map[int]map[string]string){
	o := orm.NewOrm()
	offset := (page - 1) * pageSize
	dailyReports := make([]*ReportDailyStore,0)
	count := 0

	if start == "" && end == "" {
		sqlStr := "select * from report_daily_store order by report_date desc, app_id asc limit " + strconv.Itoa(offset) + "," + strconv.Itoa(pageSize)
		o.Raw(sqlStr).QueryRows(&dailyReports)
		sqlCount := "select count(*) from report_daily_store"
		o.Raw(sqlCount).QueryRow(&count)
	} else {
		sqlStr := "select * from report_daily_store where report_date between ? and ? " +
			"order by report_date desc, app_id asc limit " + strconv.Itoa(offset) + "," + strconv.Itoa(pageSize)
		o.Raw(sqlStr, libs.StrToTime(start), libs.StrToTime(end)).QueryRows(&dailyReports)
		sqlCount := "select count(*) from report_daily_store where report_date between ? and ? "
		o.Raw(sqlCount, libs.StrToTime(start), libs.StrToTime(end)).QueryRow(&count)
	}

	results := make([]map[string]string,0)
	for k,v := range dailyReports{
		appName := ""
		if app,find := appHash[v.AppId];find{
			appName = app.AppName
		}
		result := map[string]string{
			"sort":strconv.Itoa(k+1),
			"appName":appName,
			"userNew":strconv.Itoa(v.UserNew),
			"userLogin":strconv.Itoa(v.UserLogin),
			"orderUser":strconv.Itoa(v.OrderUser),
			"orderTotal":strconv.Itoa(v.OrderTotal),
			"orderCount":strconv.Itoa(v.OrderCount),
			"orderUserNew":strconv.Itoa(v.OrderUserNew),
			"orderTotalNew":strconv.Itoa(v.OrderTotalNew),
			"orderCountNew":strconv.Itoa(v.OrderCountNew),
			"d1Retention":strconv.Itoa(v.D1Retention),
			"d7Retention":strconv.Itoa(v.D7Retention),
			"d7UserNew":strconv.Itoa(v.D7UserNew),
			"reportDate":v.ReportDate.Format("2006-01-02 15:04:05"),
		}
		results = append(results, result)
	}

	tableHead := map[int]map[string]string{
		0:{"sort":"序号",},
		1:{"reportDate":"日报时间"},
		2:{"appName":"应用名称"},
		3:{"userNew":"新增用户数"},
		4:{"userLogin":"登录用户数"},
		5:{"orderUser":"充值用户数"},
		6:{"orderTotal":"充值总额"},
		7:{"orderCount":"充值订单数"},
		8:{"orderUserNew":"[新增]充值用户数"},
		9:{"orderTotalNew":"[新增]充值总额"},
		10:{"orderCountNew":"[新增]充值订单数"},
		11:{"d1Retention":"[新增]次日留存"},
		12:{"d7Retention":"[新增]7日留存"},
	}

	return results,int64(count),tableHead
}